﻿using BookStore.Solution.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BookStore.Solution.BLL
{
   public class EntryBillDetailManager
    {
        private DataTable CreateTable(List<EntryBillDetail> list)
        {
            DataTable tb = new DataTable();
            tb.Columns.Add("EntryBillID", Type.GetType("System.Int32"));
            tb.Columns.Add("ProductID", Type.GetType("System.String"));
            tb.Columns.Add("Quatity", Type.GetType("System.Int32"));
            tb.Columns.Add("UnitPrice", Type.GetType("System.Int64"));
            tb.Columns.Add("Status", Type.GetType("System.String"));
            foreach (EntryBillDetail a in list)
            {
                DataRow newrow = tb.NewRow();
                object[] array = new object[5];
                array[0] = a.EntryBillID;
                array[1] = a.ProductID;
                array[2] = a.Quatity;
                array[3] = a.UnitPrice;
                array[4] = a.Status;
                newrow.ItemArray = array;
                tb.Rows.Add(newrow);
            }
            return tb;
        }
        public DataTable GetAll()
        {
            List<EntryBillDetail> etBillDetailList = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                etBillDetailList = (from a in ctx.EntryBillDetails where a.Status.CompareTo("Đang tồn tại") == 0 select a).ToList<EntryBillDetail>();
            }
            return CreateTable(etBillDetailList);
        }
        public DataTable SearchID(int ID)
        {
            List<EntryBillDetail> etbilldetails = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                etbilldetails = ctx.EntryBillDetails.Where(a => a.EntryBillID == ID && a.Status.CompareTo("Đang tồn tại") == 0).ToList<EntryBillDetail>();
            }
            return CreateTable(etbilldetails);
        }
        public string InsertBillDetail(EntryBillDetail b)
        {
            EntryBillDetail dt = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                try
                {
                    dt = ctx.EntryBillDetails.Where(a => a.EntryBillID == b.EntryBillID && a.Status.CompareTo("Đã xóa") == 0).FirstOrDefault<EntryBillDetail>();
                    if (dt != null)
                    {
                        dt.Status = "Đang tồn tại";
                        dt.UnitPrice = b.UnitPrice;
                        dt.Quatity = b.Quatity;
                        ctx.SaveChanges();
                    }
                    else
                    {
                        ctx.EntryBillDetails.Add(b);
                        ctx.SaveChanges();
                    }
                    return "Thêm thành công";
                }
                catch
                {
                    return "Thêm thất bại";
                }
            }
        }
        public String DeleteToBill(int entryBill)
        {
            EntryBillDetail b = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                b = ctx.EntryBillDetails.Where(a => a.EntryBillID == entryBill).FirstOrDefault<EntryBillDetail>();
                if (b != null)
                {
                    ctx.SaveChanges();
                    return "Xóa thành công!";
                }
                else
                    return "Phiếu nhập không tồn tại";
            }
        }
        public string UpdateStatus(EntryBillDetail enttrybilldt)
        {
            List<EntryBillDetail> etbilldetail = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                etbilldetail = ctx.EntryBillDetails.Where(a => a.EntryBillID == enttrybilldt.EntryBillID && a.ProductID.CompareTo(enttrybilldt.ProductID) == 0).ToList<EntryBillDetail>();
                if (etbilldetail != null)
                {
                    foreach (var item in etbilldetail)
                    {
                        item.Status = enttrybilldt.Status;
                        item.UnitPrice = enttrybilldt.UnitPrice;
                        item.Quatity = enttrybilldt.Quatity;
                        ctx.SaveChanges();
                    }
                    return "Cập nhật thành công!";
                }
                else
                    return "Cập nhật thất bại";
            }
        }
        public string UpdateStatus(EntryBill entrybill)
        {
            List<EntryBillDetail> etbilldetail = null;
            using (BookStoreEntities ctx = new BookStoreEntities())
            {
                etbilldetail = ctx.EntryBillDetails.Where(a => a.EntryBillID == entrybill.EntryBillID).ToList<EntryBillDetail>();
                if (etbilldetail != null)
                {
                    foreach (var item in etbilldetail)
                    {
                        item.Status = "Đã xóa";
                        ctx.SaveChanges();
                    }
                    return "Xóa thành công!";
                }
                else
                    return "Xóa thất bại";
            }
        }
       
    }
}
